package itman.elm.controller;

import itman.elm.po.DeliveryAddress;
import itman.elm.service.DeliveryAddressService;
import itman.elm.service.impl.DeliveryAddressServiceImpl;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.util.List;

public class DeliveryAddressController {

    // http://localhost:8080/elm/DeliveryAddressController/listDeliveryAddressByUserId?userId=1
    public Object listDeliveryAddressByUserId(HttpServletRequest request, HttpServletResponse response)throws Exception{
        String userId = request.getParameter("userId");
        DeliveryAddressService service = new DeliveryAddressServiceImpl();
        List<DeliveryAddress> list = service.listDeliveryAddressByUserId(userId);
        return list;
    }

    // http://localhost:8080/elm/DeliveryAddressController/saveDeliveryAddress?contactName=?
    // &contactSex=?&contactTel=?&address=?&userId=?
    public Object saveDeliveryAddress(HttpServletRequest request, HttpServletResponse response)throws Exception{
        DeliveryAddress deliveryAddress = new DeliveryAddress();
        deliveryAddress.setContactName(request.getParameter("contactName"));
        deliveryAddress.setContactSex(Integer.valueOf(request.getParameter("conractSet")));
        deliveryAddress.setContactTel(request.getParameter("contactTel"));
        deliveryAddress.setAddress(request.getParameter("address"));
        deliveryAddress.setUserId(request.getParameter("userId"));
        DeliveryAddressService service = new DeliveryAddressServiceImpl();
        int result = service.saveDeliveryAddress(deliveryAddress);
        return result;
    }

    public Object removeDelveryAddress(HttpServletRequest request, HttpServletResponse response)throws  Exception{
        Integer daId = Integer.valueOf(request.getParameter("daId"));
        DeliveryAddressService service = new DeliveryAddressServiceImpl();
        int result = service.removeDeliveryAddress(daId);
        return result;
    }

    // http://localhost:8080/elm/DeliveryAddressController/removeDeliveryAddress?daId=?
    public Object getDeliveryAddressById(HttpServletRequest request,HttpServletResponse response) throws Exception{
        Integer daId = Integer.valueOf(request.getParameter("daId"));
        DeliveryAddressService service = new DeliveryAddressServiceImpl();
        DeliveryAddress deliveryAddress = service.getDeliveryAddressById(daId);
        return deliveryAddress;
    }

    // http://localhost:8080/elm/DeliveryAddressController/updateDeliveryAddress?contactName=?
    // &contactSex=?&contactTel=?&address=?&userId=?
    public Object updateDeliveryAddress(HttpServletRequest request,HttpServletResponse response) throws Exception{
        DeliveryAddress deliveryAddress = new DeliveryAddress();
        deliveryAddress.setDaId(Integer.valueOf(request.getParameter("daId")));
        deliveryAddress.setContactName(request.getParameter("contactName"));
        deliveryAddress.setContactSex(Integer.valueOf(request.getParameter("contactSex")));
        deliveryAddress.setContactTel(request.getParameter("contactTel"));
        deliveryAddress.setAddress(request.getParameter("address"));
        deliveryAddress.setUserId(request.getParameter("userId"));
        DeliveryAddressService service = new DeliveryAddressServiceImpl();
        int result = service.updateDeliveryAddress(deliveryAddress);
        return result;
    }


}
